package com.example.accessconnector;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.example.accessconnector.component.BubbleToast;
import com.example.accessconnector.util.AccessDatabaseCreator;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.ResourceBundle;

public class HelloController implements Initializable {
    @FXML
    private Label welcomeText;

    @FXML
    private TextField txtAddr;
    StringProperty propAddr = new SimpleStringProperty();

    @FXML
    private TextArea txtSql;
    StringProperty propSql = new SimpleStringProperty();

    @FXML
    private TextArea txtRes;
    StringProperty propRes = new SimpleStringProperty();


    // 数据库访问
    AccessDatabaseCreator dbCreator;


    // 设置固定值
    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        propAddr.setValue((new File(new File("").getAbsolutePath(), "text.db").getAbsolutePath()));

        // 双向绑定
        txtAddr.textProperty().bindBidirectional(propAddr);
        txtSql.textProperty().bindBidirectional(propSql);
        txtRes.textProperty().bindBidirectional(propRes);

    }


    @FXML
    protected void onHelloButtonClick() {
        welcomeText.setText("Welcome to JavaFX Application!");
    }


    /**
     * 连接
     *
     * @param actionEvent
     */
    @FXML
    public void onConnectButtonClick(ActionEvent actionEvent) throws Exception {

        dbCreator = new AccessDatabaseCreator(txtAddr.getText());


        dbCreator.testConnection();

        BubbleToast.show(HelloApplication.getPrimaryStage(), "连接成功");
    }

    @FXML
    public void onCreateButtonClick(ActionEvent actionEvent) {
        dbCreator.createNewAccessDatabaseNotExists();

        BubbleToast.show(HelloApplication.getPrimaryStage(), "操作成功");
    }

    /**
     * 运行sql
     *
     * @param actionEvent
     * @throws Exception
     */
    @FXML
    public void onRunButtonClick(ActionEvent actionEvent) throws Exception {
        try {
            dbCreator.executeSql(txtSql.getText());
            BubbleToast.show(HelloApplication.getPrimaryStage(), "操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            BubbleToast.show(HelloApplication.getPrimaryStage(), e.getMessage());
        }
    }

    @FXML
    public void onRunQueryButtonClick(ActionEvent actionEvent) {
        try {
            List<HashMap<String, Object>> strings =
                    dbCreator.executeQuery(txtSql.getText());
            propRes.set(JSON.toJSONString(strings, JSONWriter.Feature.PrettyFormat));
            BubbleToast.show(HelloApplication.getPrimaryStage(), "操作成功");
        } catch (Exception e) {
            BubbleToast.show(HelloApplication.getPrimaryStage(), e.getMessage());
        }
    }

    public void onCreate(ActionEvent actionEvent) {
        propSql.set("""
                CREATE TABLE `text` (
                id VARCHAR(32),
                )
                """);
    }

    public void onInsert(ActionEvent actionEvent) {
        propSql.set("insert into test values ('1')");
    }

    public void onDel(ActionEvent actionEvent) {
        propSql.set("delete from test where id = 1");
    }

    public void onUpdate(ActionEvent actionEvent) {
        propSql.set("update test set id = '1'");
    }

    public void onSelect(ActionEvent actionEvent) {
        propSql.set("select * from test;");
    }

    public void onSelectTable(ActionEvent actionEvent) {
        propSql.set("""
                SELECT *
                FROM INFORMATION_SCHEMA.Tables;
                """);
    }
}